package com.example.pictureview;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.ListView;

import com.example.pictureview.adapter.Image9Adapter;
import com.example.pictureview.utils.Utils;
import com.example.pictureview.widget.BaseImageLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Image9BrowseActivity extends Activity {

    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image9_browse);
        mListView = (ListView) findViewById(R.id.listview);
        new ImageTask().execute();
//        mListView.setAdapter(new );
    }

    public static void gotoActivity(Context context) {
        Intent intent = new Intent(context, Image9BrowseActivity.class);
        context.startActivity(intent);
    }





    class ImageTask extends AsyncTask<Void, Void, List<List<String>>> {

        @Override
        protected List<List<String>> doInBackground(Void... params) {
            final List<List<String>> paths = new ArrayList<>();
            if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
                Uri originalUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                ContentResolver cr = Image9BrowseActivity.this.getContentResolver();
                Cursor cursor = cr.query(originalUri, null, null, null, null);
                if (cursor == null) {
                    return paths;
                }
                cursor.moveToFirst();
                while (!cursor.isAfterLast()) {
                    Random random = new Random();
                    int n = random.nextInt(9) + 1;
                    List<String> list = new ArrayList<>(n);
                    for (int i = 0; i < n; ++i) {
                        if (!cursor.isAfterLast()) {
                            String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
                            list.add(path);
                            cursor.moveToNext();
                        } else {
                            break;
                        }
                    }
                    paths.add(list);
                }
                cursor.close();
            }
            return paths;
        }

        @Override
        protected void onPostExecute(List<List<String>> strings) {
            mListView.setAdapter(new Image9Adapter(Image9BrowseActivity.this, new MyImageLoader(), strings));

        }
    }

    private static class MyImageLoader extends BaseImageLoader {

        @Override
        protected Bitmap getBitmapFormUrl(String url) {
            return Utils.decodeBitmapFromFile(url, 100, 100);
        }
    }
}
